
package com.bluelinelabs.conductor.conductor;


import ohos.aafwk.content.IntentParams;

import com.bluelinelabs.conductor.conductor.internal.TransactionIndexer;

/**
 * lipeiquan
 *
 * @since 2021-05-06
 */
public class RouterTransaction {

    private int INVALID_INDEX = -1;
    private String KEY_VIEW_CONTROLLER_BUNDLE = "RouterTransaction.controller.bundle";
    private String KEY_PUSH_TRANSITION = "RouterTransaction.pushControllerChangeHandler";
    private String KEY_POP_TRANSITION = "RouterTransaction.popControllerChangeHandler";
    private String KEY_TAG = "RouterTransaction.tag";
    private String KEY_INDEX = "RouterTransaction.transactionIndex";
    private String KEY_ATTACHED_TO_ROUTER = "RouterTransaction.attachedToRouter";

    public Controller controller;
    private String tag;
    private ControllerChangeHandler pushControllerChangeHandler;
    private ControllerChangeHandler popControllerChangeHandler;
    private boolean attachedToRouter;
    private int transactionIndex = INVALID_INDEX;


    public RouterTransaction(Controller controller) {
        this.controller = controller;
    }

    public RouterTransaction(IntentParams bundle) {
        controller = Controller.newInstance((IntentParams) bundle.getParam(KEY_VIEW_CONTROLLER_BUNDLE));

    }


    public static RouterTransaction with(Controller controller) {
        return new RouterTransaction(controller);
    }

    public void ensureValidIndex(TransactionIndexer indexer) {
        if (transactionIndex == INVALID_INDEX) {
            transactionIndex = indexer.nextIndex();
        }
    }


    public ControllerChangeHandler pushChangeHandler() {
        return controller.getOverriddenPushHandler() != null ? controller.getOverriddenPushHandler() : pushControllerChangeHandler;
    }


    public RouterTransaction pushChangeHandler(ControllerChangeHandler handler) {
        if (!attachedToRouter) {
            pushControllerChangeHandler = handler;
            return this;
        } else {
//            throw RuntimeException("${javaClass.simpleName}s can not be modified after being added to a Router.");
        }
        return null;
    }


    public ControllerChangeHandler popChangeHandler() {

        return controller.getOverriddenPopHandler() != null ? controller.getOverriddenPopHandler() : popControllerChangeHandler;
    }


    public RouterTransaction popChangeHandler(ControllerChangeHandler handler) {
        if (!attachedToRouter) {
            popControllerChangeHandler = handler;
            return this;
        } else {
//            throw RuntimeException("${javaClass.simpleName}s can not be modified after being added to a Router.")
        }
        return null;
    }


    public int getTransactionIndex() {
        return transactionIndex;
    }

    public void setTransactionIndex(int transactionIndex) {
        this.transactionIndex = transactionIndex;
    }


    public void onAttachedToRouter() {
        System.out.println("wtf onAttachedToRouter ");
        attachedToRouter = true;
    }

    public String tag() {
        return tag;
    }


    public RouterTransaction tag(String tag) {
        if (!attachedToRouter) {
            this.tag = tag;
            return this;
        } else {
//            throw RuntimeException(javaClass.simpleName + "s can not be modified after being added to a Router.")
            System.out.println(this.getClass().getSimpleName() + "s can not be modified after being added to a Router.");
        }
        return null;
    }

    public IntentParams saveInstanceState() {
        IntentParams intentParams = new IntentParams();
        intentParams.setParam(KEY_VIEW_CONTROLLER_BUNDLE, controller.saveInstanceState());
        if (pushControllerChangeHandler != null) {
            intentParams.setParam(KEY_PUSH_TRANSITION, pushControllerChangeHandler.toBundle());
        }
        if (popControllerChangeHandler != null) {
            intentParams.setParam(KEY_POP_TRANSITION, popControllerChangeHandler.toBundle());
        }
        intentParams.setParam(KEY_TAG, tag);
        intentParams.setParam(KEY_INDEX, transactionIndex);
        intentParams.setParam(KEY_ATTACHED_TO_ROUTER, attachedToRouter);
        return intentParams;
    }


}
